// pages/users/order/index.js
const {
  cardList,
  cardScoreUpdate,
  cardGetUse,
  cardUse,
  volunteerOrderList,
  courseOrderList,
} = require('../../../api/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    navList: [{
      name: '会员订单'
    }, {
      status: 0,
      name: '志愿订单'
    }, {
      status: 1,
      name: '课程订单'
    }],
    showModal: false,
    tabIndex: 0,
    pageNo: 1,
    pageSize: 20,
    type: null,
    loading:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getList()
  },
  changeIndex(e) {
    const {
      index,
      type,
    } = e.currentTarget.dataset
    if (index == this.data.tabIndex) return
    this.setData({
      [type]: parseInt(index),
    })
    this.getList(1)

  },
  //我的优惠券
  async getList(page) {
    if (page) {
      this.setData({
        pageNo: page,
        list: []
      })
    }
    const {
      list,
      pageNo,
      pageSize,
      tabIndex
    } = this.data
    const params = {
      pageNo,
      pageSize,
    }
    var result = {}
    switch (tabIndex) {
      case 1:
        result = await volunteerOrderList(params)
        break;
      case 2:
        result = await courseOrderList(params)
        break;
      default:
        result = await cardList(params)
        break;
    }
    const {
      records,total
    } = result.result
    if (records.length == 0) return
    records.map((item) => {
      item['secondSelectChar'] = item.secondSelect && item.secondSelect.replaceAll(',', '/')
    })
    this.setData({
      list: list.concat(records),
      pageNo: pageNo + 1,
      total
    })
  },
  openModal(e) {
    const {
      value,
      index,
      id
    } = e.currentTarget.dataset
    this.setData({
      showModal: true,
      initData: value,
      id,
      currentIndex: parseInt(index)
    })
  },
  async onLoadFunc(e) {
    const {
      type
    } = this.data
    if (type == 'use') {
      this.use(e.detail)
    } else {
      this.updateScore(e.detail)
    }

  },
  async updateScore(value) {
    const {
      province,
      provinceId,
      score,
      ranking,
      firstSelect,
      secondSelect,
    } = value
    const {
      id
    } = this.data
    const params = {
      province,
      provinceId,
      score,
      ranking,
      firstSelect,
      secondSelect,
      id
    }
    const result = await cardScoreUpdate(params)
    if (result.success) {
      wx.showToast({
        title: result.message,
        icon: 'none'
      })
      this.getList(1)
    }


  },
  async use(value) {
    const {
      score,
      ranking,
      firstSelect,
      secondSelect,
      province,
      provinceId
    } = value
    const {
      id
    } = this.data
    const params = {
      province,
      provinceId,
      score,
      ranking,
      firstSelect,
      secondSelect,
      id
    }
    const result = await cardUse(params)
    await this.getList(1)
    if (result.success) {
      this.navigate(result.result)
    }
  },
  navigate(result) {
    const params = result
    params.level1Name = result.useBatch == '本科批' ? '本科' : '专科（高职）'
    wx.navigateTo({
      url: '/pages/volunteer/select/index?params=' + encodeURIComponent(JSON.stringify(params))
    })

  },
  checkUse(e) {
    const {
      value,
      index,
      id
    } = e.currentTarget.dataset
    this.setData({
      id,
      type: 'use',
      currentIndex: parseInt(index)
    })
    if (!value.secondSelect) {
      this.setData({
        showModal: true,
        initData: value
      })
    } else {
      this.use(value)
    }
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const {
      list,
      total
    } = this.data
    if (list.length < total) {
      this.getList()
    }
  },

})